/*not use it anymore*/

package fcc.Seng462project.client;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratedTabPanel;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
//added by steve
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class MainPage {
	RootPanel rootPanel = RootPanel.get();
	private Button uploadbutton;
	private Button NewFolderButton;
	private Button shareButton;
	private FlexTable file_FlexTable;
	private FlexTable mainFlexTable;
	private Button filesButton;
	private Button sharingbutton;
	private Button helpButton;
	int row,col;

    // Use an RPC call to the Blob Service to get the blobstore upload url
    BlobServiceAsync blobService = GWT.create(BlobService.class);
	
	
	public void mainButton(){
		HTML html= new HTML("<h1>FileCrate</h1>");
		//Label logLabel = new Label("FileCrate");
		rootPanel.add(html);
		DecoratedTabPanel tb=tabpanel();
		rootPanel.add(tb,300,300);
		//logLabel.setSize("60", "20");
		
		mainFlexTable = new FlexTable();
		rootPanel.add(mainFlexTable, 107, 65);
		mainFlexTable.setSize("100px", "100px");
		//mainFlexTable.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		file_FlexTable = new FlexTable();
		mainFlexTable.setWidget(0, 0, file_FlexTable);
		VerticalPanel verticalPanel = new VerticalPanel();
		rootPanel.add(verticalPanel, 0, 65);
		verticalPanel.setSize("100px", "100px");
		//create a dialogbox
		final DialogBox dialogBox;
		dialogBox = createDialogBox();
		dialogBox.setText("Upload your file");
		/*create a clickhandler for button Files*/
		
		
		
		filesButton = new Button("Files");
		filesButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				clean();
				uploadbutton = new Button("Upload");
				uploadbutton.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						dialogBox.center();
						dialogBox.show();
					}
				});
				file_FlexTable.setWidget(0, 0, uploadbutton);
		
				NewFolderButton = new Button("New Folder");
				file_FlexTable.setWidget(0, 1, NewFolderButton);
				NewFolderButton.setWidth("117px");
				col++;
				shareButton = new Button("Share a folder");
				file_FlexTable.setWidget(0, 2, shareButton);
				shareButton.setWidth("116px");
				col++;
				
			}
		});
		verticalPanel.add(filesButton);
		filesButton.setWidth("100px");
		
		sharingbutton = new Button("Sharing");
		
		//create clickHandler for share a folder button
		sharingbutton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				clean();
				Button sharefolder= new Button("Share a folder");
				file_FlexTable.setWidget(0,1,sharefolder);
				sharefolder.setWidth("116px");
				Label name=new Label("Folder Name");
				file_FlexTable.setWidget(1, 0, name);
				row++;
				
			}
		});
		verticalPanel.add(sharingbutton);
		sharingbutton.setWidth("99px");
		
		helpButton = new Button("Help");
		//create clickhandler for help button
		helpButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				clean();
				HTML html= new HTML("This is test, i don;t know what i should add it");
				file_FlexTable.setWidget(1,0,html);
			}
		});
		verticalPanel.add(helpButton);
		helpButton.setWidth("101px");
	}
	
	//mainFlexTable.add(tb);
	//ignore here
	public void clean(){
		for(int i=0;i<row+1;i++){
			for(int j=0;j<col+1;j++){
				file_FlexTable.setWidget(i,j,null);
			}
		}
		
	}
	//dialogbox layout and interface. 
	//file path goes here
	public DialogBox createDialogBox(){
   //all stuff related to the GWT dialogbox used to upload files
		final DialogBox dialBox = new DialogBox();
		dialBox.setText("Upload a file");
		//dialogBox.setAnimationEnabled(true);
		VerticalPanel dialogContents=new VerticalPanel();
		VerticalPanel formPanel = new VerticalPanel();
		HorizontalPanel hp = new HorizontalPanel();
		HorizontalPanel hbutton = new HorizontalPanel();
		FileUpload upload = new FileUpload();
		final FormPanel uploadForm = new FormPanel();
		uploadForm.setEncoding(FormPanel.ENCODING_MULTIPART);
	    uploadForm.setMethod(FormPanel.METHOD_POST);
	    
		dialBox.setWidget(dialogContents);
		
	    hp.add(upload);
	    upload.setName("upload");
	    Button submitButton=new Button("Submit");
		Button cancelButton = new Button("Cancel", new ClickHandler(){
			public void onClick(ClickEvent event){
				dialBox.hide();
			}
		});
		hp.add(submitButton);
		hp.add(cancelButton);
		uploadForm.add(hp);
		uploadForm.setWidget(hp);
		
		dialogContents.setSpacing(2);
		dialogContents.add(uploadForm);
		dialogContents.setCellHorizontalAlignment(hp, HasHorizontalAlignment.ALIGN_CENTER);

		
		submitButton.addClickHandler(new ClickHandler() {
		      @Override
		      public void onClick(ClickEvent event) {

		        blobService.getBlobStoreUploadUrl(new AsyncCallback<String>() {

		              @Override
		              public void onSuccess(String result) {
		                // Set the form action to the newly created
		                // blobstore upload URL
		                uploadForm.setAction(result);

		                // Submit the form to complete the upload
		                uploadForm.submit();
		                uploadForm.reset();
		                dialBox.hide();
		              }

		              @Override
		              public void onFailure(Throwable caught) {
		                caught.printStackTrace();
		                System.out.println("Upload fail!");
		              }
		            });

		      }
		    });
		
		uploadForm.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
          @Override
          public void onSubmitComplete(SubmitCompleteEvent event) {
            //test println to make sure it gets the ID of the upload 
            System.out.println(event.getResults());

          }

        });
		
		return dialBox;
	}
	/*this is tab , it include welcome table, file tab, sharing a folder tab and help tab*/
	public DecoratedTabPanel tabpanel(){
		DecoratedTabPanel tabPanel = new DecoratedTabPanel();
		tabPanel.setWidth("400px");
		String[] tabTitles={"Welcome","Files","Sharing","Help"};
		HTML homeText= new HTML("Welcome to FileCrate");
		FlexTable welcome=welcometab();
		//add welcome tab
		tabPanel.add(welcome,tabTitles[0]);
		
		//add file table
		HTML file_text=new HTML("This is file button");
		VerticalPanel fileLabel=file_manage();
		tabPanel.add(fileLabel,tabTitles[1]);
		//add sharing table
		HTML sharing_text = new HTML("This is sharing text, I don;t" +
				"hahahhaahahhahahahhahahahahahahahahahahahahahahahaha know what i s");
		tabPanel.add(sharing_text,tabTitles[2]);
		//add help table
		HTML help_text = new HTML("read instruction");
		tabPanel.add(help_text,tabTitles[3]);
		//return the content
		tabPanel.selectTab(0);
		/*
		VerticalPanel vPanel2 = new VerticalPanel();
		vPanel2.add(new HTML("<h2>FILE CRATE<h2>"));
		vPanel2.add(tabPanel);
		
		*/
		return tabPanel;
	}
	/*this is welcome table layout, 
	 * it will show current user name, number of file,number of shaing file
	 * paramter: user name,numberoffile,numberofsharingfile
	 * return flextable welcome_layout*/
	public FlexTable welcometab(){
		Label userLabel=new Label("User: ");
		Label totalFileLabel = new Label("Total File: ");
		Label sharingFileLabel = new Label("Total sharing File: ");
		FlexTable welcome_layout = new FlexTable();
		welcome_layout.setHTML(0,0,"Welcome to FileCrate");
		welcome_layout.setWidget(1,0,userLabel);
		welcome_layout.setWidget(2,0,totalFileLabel);
		welcome_layout.setWidget(3, 0, sharingFileLabel);
		
		
		
	
		Label uLabel=new Label("Yaning Zhai ");
		Label tFileLabel = new Label("20 ");
		Label sFileLabel = new Label("10 ");
		
		welcome_layout.setWidget(1,1,uLabel);
		welcome_layout.setWidget(2,1,tFileLabel);
		welcome_layout.setWidget(3, 1, sFileLabel);
		return welcome_layout;
		
	}
	public VerticalPanel fileTab(){
		//create a dialogbox
		final DialogBox dialogBox;
		dialogBox = createDialogBox();
		dialogBox.setText("Upload your file");
		
		Label uploadLabel = new Label("Upload");
		uploadLabel.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.center();
				dialogBox.show();
				
			}
		});
		Label newFolderLabel= new Label("New Foler");
		Label sharingFolerLabel = new Label("Share a folder"); 
		HorizontalPanel labelPanel_1 = new HorizontalPanel();
		HorizontalPanel labelPanel_2=new HorizontalPanel();
		VerticalPanel file_Layout = new VerticalPanel();
		//HorizontalPanel labelPanel_3= new HorizontalPanel();
		labelPanel_1.add(uploadLabel);
		labelPanel_1.add(newFolderLabel);
		labelPanel_1.add(sharingFolerLabel);
		labelPanel_1.setSize("500px", "30px");
		labelPanel_1.addStyleName("labelPanel-List");
		
		Label nameLabel= new Label("File Name");
		Label sizeLabel=new Label("Size");
		Label modifiedLabel=new Label("Last Modified");
		
		labelPanel_2.add(nameLabel);
		labelPanel_2.add(sizeLabel);
		labelPanel_2.add(modifiedLabel);
		labelPanel_2.setSize("500px", "20px");
		
		file_Layout.add(labelPanel_1);
		file_Layout.add(labelPanel_2);
		
		
		return file_Layout;
		
		
	}
	/*we need to manage if the resource is a folder or a file
	 * also, we need to show up all the information for the label*/
	public VerticalPanel file_manage(){
		VerticalPanel file_layout=fileTab();
		
		return file_layout;
	}
	
	

}
